package com.umakr.ax.app.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.umakr.ax.common.mapper.ShowDiscussMapper;
import com.umakr.ax.common.model.RadioStationShow;
import com.umakr.ax.common.model.ShowDiscuss;
import com.umakr.ax.common.service.RadioStationFollowerService;
import com.umakr.ax.common.service.RadioStationShowService;
import com.umakr.ax.common.service.ShowDiscussService;
import com.umakr.ax.common.service.ShowFabulousService;
import com.umakr.ax.common.vo.ShowDiscussVO;
import com.umakr.ax.core.base.BaseService;
import com.umakr.ax.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class ShowDiscussWebService  extends BaseService<ShowDiscussMapper, ShowDiscuss> {

    @Autowired
    ShowDiscussService showDiscussService;
    @Autowired
    RadioStationShowService radioStationShowService;
    @Autowired
    ShowFabulousService showFabulousService;
    @Autowired
    RadioStationFollowerService radioStationFollowerService;
    @Autowired
    Environment env;

    public Object updateShowDiscussReviewStatus(Long discussId,Integer reviewStatus ){
        int code = 0;
        String msg = "";

        if(reviewStatus == 0 || reviewStatus == 1){
            if(discussId != null){
                ShowDiscuss sd = queryById(discussId);
                if(sd != null){
                    if(((Integer)0).equals(sd.getDelStatus())){
                        sd.setReviewStatus(reviewStatus);
                        update(sd);
                        code = 1;
                        msg = "审核成功";
                        //审核成功后需要更新秀的评论数据
                        RadioStationShow radioStationShow =  radioStationShowService.queryById(sd.getShowId());
                        if(radioStationShow != null){
                            EntityWrapper<ShowDiscuss> showDiscussWrapper = new EntityWrapper<>();
                            showDiscussWrapper.eq("show_id",sd.getShowId());
                            showDiscussWrapper.eq("del_status",0);
                            //如果是审核后才能评论的话，需要新增一个筛选条件
                            if(radioStationShow.getShowReviewPermissions() == 1){
                                showDiscussWrapper.eq("review_status",1);
                            }
                            Integer showDiscussCount = selectCount(showDiscussWrapper);
                            radioStationShow.setCommentCount(showDiscussCount);
                            radioStationShowService.updateRadioStationShow(radioStationShow);
                        }
                    }else{
                        msg = "评论不存在或者已被删除";
                    }
                }else{
                    msg = "评论不存在或者已被删除";
                }
            }else{
                msg = "评论id不能为空";
            }
        } else{
            msg = "审核状态错误";
        }

        return resultMap(code,msg);
    }


    public Object listShowDiscuss( Long showId ){
        String aliUrl = env.getProperty("oss.img.path");
        String defaultAvatar = env.getProperty("user.default.avatar");

        Page<ShowDiscussVO> page = baseMapper.listShowDiscuss(showId);
        for (ShowDiscussVO showDiscussVO : page.getRecords()){
            showDiscussVO.setHeadImgUrl(showDiscussVO.getHeadImgUrl() == null ?  aliUrl+defaultAvatar : aliUrl+showDiscussVO.getHeadImgUrl());
        }
        RadioStationShow show = radioStationShowService.queryById(showId);
        WebUtil.addParamToPage(page,"likeCount",show.getLikeCount());
        WebUtil.addParamToPage(page,"commentCount",show.getCommentCount());

        return page;
    }

    public Object delShowDiscuss( Long discussId ){
        int code = 0;
        String msg = "";
        Long showId = null;
        if(discussId != null){
            ShowDiscuss sd = queryById(discussId);
            if(sd != null){
                showId = sd.getShowId();
                sd.setDelStatus(1);
                update(sd);
                code = 1;
                msg = "评论删除成功";
                //审核成功后需要更新秀的评论数据
                RadioStationShow radioStationShow =  radioStationShowService.queryById(sd.getShowId());
                if(radioStationShow != null){
                    EntityWrapper<ShowDiscuss> showDiscussWrapper = new EntityWrapper<>();
                    showDiscussWrapper.eq("show_id",sd.getShowId());
                    showDiscussWrapper.eq("del_status",0);
                    //如果是审核后才能评论的话，需要新增一个筛选条件
                    if(radioStationShow.getShowReviewPermissions() == 1){
                        showDiscussWrapper.eq("review_status",1);
                    }
                    Integer showDiscussCount = selectCount(showDiscussWrapper);
                    radioStationShow.setCommentCount(showDiscussCount);
                    radioStationShowService.updateRadioStationShow(radioStationShow);
                }
            }else{
                msg = "未找到相关评论";
            }
        }else{
            msg = "评论已被删除";
        }
        return resultMap(code,msg);
    }



}
